﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MachineQA.Model;
using MachineQA.ViewModel;

namespace MachineQA.View
{
    /// <summary>
    /// Interaction logic for MachineTab.xaml
    /// </summary>
    public partial class MachinesView : UserControl
    {
        public MachinesView()
        {
            InitializeComponent();
        }

        private void NewMachineNameBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TryEnableSaveButton();
        }

        private void TryEnableSaveButton()
        {
            MachineSaveButton.IsEnabled = (NewMachineNameBox.Text != string.Empty && LocationCombobox.SelectedItem != null &&
                                          (Constants.entities.Machines.
                                          Where(
                                            (i) => i.Name == NewMachineNameBox.Text &&
                                            i.Location.Name == 
                                            ((LocationViewModel)LocationCombobox.SelectedValue).Name
                                          ).Count() == 0));
        }

        private void LocationCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((MachinesViewModel)this.DataContext).NewMachine.Location = (LocationViewModel)LocationCombobox.SelectedItem;
            TryEnableSaveButton();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(((MachinesViewModel)this.DataContext).NewMachine.Name);
        }

        private void MachinesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0 && e.AddedItems[0] is MachineViewModel)
                ((MachinesViewModel)this.DataContext).SelectedMachine = (MachineViewModel)e.AddedItems[0];
        }

        private void MachineDeleteButton_Click(object sender, RoutedEventArgs e)
        {
            MachinesList.UpdateLayout();
        }
    }
}
